package com.fessc.develop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fessc.develop.entity.dbvo.TBatchTimerDef;
import com.fessc.develop.job.ScheduleJob;
import com.fessc.develop.mapper.TBatchTimerDefMapper;
import com.fessc.develop.service.interfaces.ITBatchTimerDefService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fessc.develop.utils.BusiUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author y
 * @since 2024-07-08
 */
@Slf4j
@Service
public class TBatchTimerDefServiceImpl extends ServiceImpl<TBatchTimerDefMapper, TBatchTimerDef> implements ITBatchTimerDefService {

    @Resource
    private List<ScheduleJob> jobList;

    private static Map<String, ScheduleJob> jobMap;

    /**
     * 定时任务启动加载
     */
    @PostConstruct
    void init() {
        jobMap = jobList.stream().collect(Collectors.toMap(ScheduleJob::getTaskCode, p -> p, (v1, v2) -> v1));
        log.info("==============加载定时任务==============" + jobMap.keySet());

        //查询生效的定时任务
        List<TBatchTimerDef> batchTimerDefList = this.list(new QueryWrapper<TBatchTimerDef>().eq("STATUS", "ON"));
        if (BusiUtil.isNotNull(batchTimerDefList)) {
            Map<String, TBatchTimerDef> batchTimerDefMap = batchTimerDefList.stream().collect(Collectors.toMap(TBatchTimerDef::getTaskCode, p -> p, (v1, v2) -> v1));
            for (Map.Entry<String, ScheduleJob> entry : jobMap.entrySet()) {
                TBatchTimerDef tBatchTimerDef = batchTimerDefMap.get(entry.getKey());
                if (BusiUtil.isNotNull(tBatchTimerDef)) {
                    entry.getValue().setCronExp(tBatchTimerDef.getCronExpression());
                    entry.getValue().startCron();
                }
            }
        }

    }


    /**
     * 启动定时任务
     * @param taskCode 定时任务编号
     */
    @Override
    public void startCron(String taskCode) {
        if (BusiUtil.isNotNull(jobMap) && BusiUtil.isNotNull(jobMap.get(taskCode))) {
            try {
                jobMap.get(taskCode).startCron();
                log.info("启动定时任务成功：" + taskCode);
            } catch (Exception e) {
                log.info("启动定时任务失败：" + e.getMessage());
            }
        }
    }

    /**
     * 停止定时任务
     * @param taskCode 定时任务编号
     */
    @Override
    public void stopCron(String taskCode) {
        if (BusiUtil.isNotNull(jobMap) && BusiUtil.isNotNull(jobMap.get(taskCode))) {
            try {
                jobMap.get(taskCode).stopCron();
                log.info("停止定时任务成功：" + taskCode);
            } catch (Exception e) {
                log.info("停止定时任务失败：" + e.getMessage());
            }
        }
    }

    /**
     * 手动执行任务
     * @param taskCode 定时任务编号
     */
    @Override
    public void manuallyInvoke(String taskCode) {
        if (BusiUtil.isNotNull(jobMap) && BusiUtil.isNotNull(jobMap.get(taskCode))) {
            try {
                jobMap.get(taskCode).execute();
                log.info("手工执行定时任务成功：" + taskCode);
            } catch (Exception e) {
                log.info("手工执行定时任务失败：" + e.getMessage());
            }
        }
    }
}
